<?php

/**
 * 
 */
 
use Workerman\Worker;
use Workerman\Mysql;

 class mod_settings_groups{
	
	public function entry($param){
		return $this->do_mod_settings_groups($param);
	}

		
	/** 
	* do_mod_settings_user  
	* 
	* @access private 
	* @param $param batch
	* @since 1.0 
	* @return array(errcode,errmsg,rtv) 
	*/
	private function do_mod_settings_groups($param){
		$action = $param['action'];
		var_dump("s-action:" . $action ."================");

	
		if(empty($action) || $action==""){
			
			$sql = "select id, name, class, note, if( stat=1, '开启', '关闭') as stat, create_date, authority,authority_class from hf_mes_groups;";
			$dp=sql_queryidx($sql);
		
			
			//$obj = "{\"data\": " . json_encode($dp) . "}" ;
			$obj = new stdClass();
            $obj-> data = $dp;
		}


		if($action=="get_module_info"){
			$sql= "select id,name,code,class,elements,elemtext from hf_mes_modules ;";
			$dp = sql_query($sql);
			$id = $_POST["id"];
			$sql1 = "select authority,authority_class from hf_mes_groups where id= {$id};";
			$dp1=sql_fetchall($sql1);

			$authority = $dp1['authority'];
			$authority_class = $dp1['authority_class'];

			$authority = gettype($authority)==='string'? json_decode($authority) : $authority ;
			$authority_class = gettype($authority_class)==='string'? json_decode($authority_class) : $authority_class ;
			foreach ($dp as $key => $value) {
				$module_display = true;
				$code = $value["code"];
				$elements = $value["elements"];
				$elements = gettype($elements)==="string"?json_decode($elements):$elements ;
				$element_display = [];

				foreach ($elements as $k => $ele) {
					$ele_display = true;
					if($ele && in_array($ele, $authority)){
						$ele_display = false;
					}
					array_push($element_display, $ele_display);
				}

				if($code && in_array( $code, $authority_class)){
					$module_display = false;
				}
				// if($code=='mod_order_information_management')var_dump($code,$module_display); //debug
 
				$dp[$key]['module_display']= $module_display ;
				$dp[$key]['element_display']= $element_display ;
			}
			//$obj = "{\"data\": " . json_encode($dp) . "}" ;
			$obj = new stdClass();
            $obj-> data = $dp;
		}

		//保存修改
		if($action == "groups_config_edit"){
			$id = $_POST["id"];
			$authority = $_POST["authority"];
			$authority_class = $_POST["authority_class"];
			$rt = sql_update("hf_mes_groups", array('authority'=>$authority,'authority_class'=>$authority_class), array('id'=>$id));
			$obj ="done";
		}


		if($action == "add"){
			$name = $_POST["name"];
			$class = $_POST["class"];
			$stat = $_POST["stat"];
			$note = $_POST["note"];
			sql_insert("hf_mes_groups",array('name'=>$name,'class'=>$class,'stat'=>$stat,'note'=>$note,'authority'=>'[]','authority_class'=>'[]'));
			$obj = "done";
		}

		if($action == "edit"){
			$id = $_POST["id"];
			$name = $_POST["name"];
			$class = $_POST["class"];
			$stat = $_POST["stat"];
			$note = $_POST["note"];
			sql_update("hf_mes_groups", array('name'=>$name,'class'=>$class,'stat'=>$stat,'note'=>$note), array('id'=>$id));
			$obj = "done";
			
		}
		//删除角色
		if($action == "delete"){
			$id = $_POST["id"];
			sql_delete("hf_mes_groups", array('id'=>$id));
			sql_query( "update hf_mes_user set `groups` =1 where `groups` = $id " );
			$obj = "done";
		}

		/*For Groups Config*/
		if($action=="groups-config-show"){
			$sql = "select id, name, code, class, elements,elemtext from hf_mes_modules;";
			$dp=sql_query($sql);
			//$obj = "{\"data\": " . json_encode($dp) . "}" ;
			$obj = new stdClass();
            $obj-> data = $dp;
		}

		// //保存修改
		// if($action == "groups-config-edit"){
		// 	$id = $_POST["id"];
		// 	$authority = $_POST["authority"];
		// 	$authority_class = $_POST["authority_class"];
		// 	sql_update("hf_mes_groups", array('authority'=>$authority,'authority_class'=>$authority_class), array('id'=>$id));
		// 	$obj = "done";
		// }

		// if($action=="groups-config-show-detail"){
		// 	$id=$_POST["id"];
		// 	$sql = "select id,elements, elemtext from hf_mes_modules where id = '$id';";
		// 	$dp=sql_query($sql);
		// 	$obj = "{\"data\": " . json_encode($dp) . "}" ;
		// }

		// //获取code
		// if($action == "groups-config-code"){
		// 	$sql = "select code from hf_mes_modules;";
		// 	$dp=sql_query($sql);
			
		// 	$obj = json_encode($dp);
		// }

		// //获取 element
		// if($action == "groups-config-element"){
		// 	$sql = "select elements from hf_mes_modules;";
		// 	$dp=sql_query($sql);
		// 	// var_dump($dp);
		// 	// $obj = "{\"data\": " . json_encode($dp) . "}" ;
		// 	$obj = json_encode($dp);
			
		// }

		// //all_btn_selete
		// if($action == "all_btn_selete"){
		// 	$obj = $id=$POST['id'];

		// 	$sql = "select authority from hf_mes_groups where id={$id};";
		// 	// $obj = $sql;
		// 	$dp=sql_query($sql);
		// 	// var_dump($dp);
		// 	// forEach($dp as $value){
		// 		// var_dump($value);
		// 	// }
		// 	// 
		// 	// $obj = "{\"data\": " . json_encode($dp) . "}" ;
		// 	$obj = json_encode($dp);
		// }

		//return array('rtval'=>$obj);
		return array("errcode"=> 0,"errmsg"=>"","rtval"=>$obj);
	}

	/** 
	* create  
	* 通过 call_user_func 获得本class的实例
	* 
	* @access public 
	* @since 1.0 
	* @return object 本类的实例
	*/	
    public static function create(){  
        static $instance;
        if (!$instance){  
			$instance = new mod_settings_groups();  
        }
		return $instance;  
    }   
 }